%% Copyright (C) 2007 Vijay Gajula <gajulak@gmail.com>.
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% 1. Redistributions of source code must retain the above copyright
%%    notice, this list of conditions and the following disclaimer.
%% 2. Redistributions in binary form must reproduce the above
%%    copyright notice, this list of conditions and the following
%%    disclaimer in the documentation and/or other materials provided
%%    with the distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
%% OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
%% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
%% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
%% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
%% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
%% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-module(db).
-vsn(1.0).
-author('gajulak@gmail.com').
-export([start/0, stop/0, 
	 get_interface/0, get_interface/1,
	 get_route/0, get_route/1,
	 get_user/0, get_user/1,
	 set_record/1,
	 setup_defaults/0]).

-include("netconf.hrl").
-include_lib("stdlib/include/qlc.hrl").
-define(DATA_DIR, "/home/vj/tmp").

setup_db() ->
	application:load(mnesia),
	application:set_env(mnesia, dir, ?DATA_DIR),
	mnesia:create_schema([node()]),
	application:start(mnesia),
	%% mnesia:start(),
	mnesia:create_table(device, [{attributes, record_info(fields, device)},
					{disc_copies, [node()]}]),
	mnesia:create_table(route, [{attributes, record_info(fields, route)},
					{disc_copies, [node()]}]),
	mnesia:create_table(interface, [{attributes, record_info(fields, interface)},
					{disc_copies, [node()]}]),
	mnesia:create_table(dhcpserver, [{attributes, record_info(fields, dhcpserver)},
					{disc_copies, [node()]}]),
	mnesia:create_table(dnsserver, [{attributes, record_info(fields, dnsserver)},
					{disc_copies, [node()]}]),
	mnesia:create_table(user, [{attributes, record_info(fields, user)},
					{disc_copies, [node()]}]),
	mnesia:create_table(ipsecvpn, [{attributes, record_info(fields, ipsecvpn)},
					{disc_copies, [node()]}]),
	mnesia:stop().

start() ->
	application:load(mnesia),
	application:set_env(mnesia, dir, ?DATA_DIR),
	application:start(mnesia),
	mnesia:wait_for_tables([device, route, interface], 20000).

stop() ->
	mnesia:stop().

delete_db() ->
	application:load(mnesia),
	application:set_env(mnesia, dir, ?DATA_DIR),
	mnesia:delete_schema([node()]).

setup_defaults() ->
	delete_db(),
	setup_db(),
	start(),
	setup_interface(),
	setup_user(),
	stop().

do(Q) ->
	F = fun() -> qlc:e(Q) end,
	{atomic, Val} = mnesia:transaction(F),
	Val.

setup_interface() ->
	Int = [
	 { interface, "eth2", "192.168.100.244", "255.255.255.0",
		     internal, 1500, enabled },
	 { interface, "eth1", "172.16.10.244", "255.255.255.0",
		     dmz, 1500, enabled },
	 { interface, "eth0", "10.10.10.244", "255.255.255.0",
		     external, 1500, enabled }
	],
	F = fun() -> lists:foreach(fun(X) -> mnesia:write(X) end, Int) end,
	mnesia:transaction(F).

setup_user() ->
	Rec = { user, "admin", "admin", never, yes, yes, "", yes, yes, yes,
		10,  "", "", "", enable },
	set_record(Rec).

get_interface() ->
	do(qlc:q([X || X <- mnesia:table(interface)])).

get_interface(Name) ->
	do(qlc:q([X || X <- mnesia:table(interface), X#interface.name == Name])).

get_route() ->
	do(qlc:q([X || X <- mnesia:table(route)])).

get_route(Ip) ->
	do(qlc:q([X || X <- mnesia:table(route), X#route.address == Ip])).

get_user() ->
	do(qlc:q([X || X <- mnesia:table(user)])).

get_user(User) ->
	do(qlc:q([X || X <- mnesia:table(user), X#user.name == User])).

set_record(Rec) ->
	List = [Rec],
	F = fun() -> lists:foreach(fun(X) -> mnesia:write(X) end, List) end,
	mnesia:transaction(F).
